package Classes.GameUtils.LinkAction
{
	import Classes.GameDatas.GD_Building_Sundry;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Zombie;
	import Classes.GameErrors.GameError;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building_Sundry;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_HavenWall;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapTarget;
	import Classes.GameViews.ViewPanels.VP_FastBuy_actPoint;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	/**
	 *连续行动 
	 * @author 邓坚
	 * 
	 */	
	public class LinkAction
	{
		public var actTargets:Array;//Vector.<MapTarget>;						//等待行动的对像
		
		private var linkTimer:Timer	=	null;						//内部定时器
		
		private var actionDis:uint		=	1000;						//行动间隔		1000 = 1秒
		
		private var mmt_character:MMT_Character;						//目标角色
		
		public function LinkAction(mmt:MMT_Character)
		{
			this.mmt_character		=	mmt;
			this.actTargets			=	new Array;//Vector.<MapTarget>;
		}
		
		/**
		 *清空对列 
		 */
		public function cleanTargets():void
		{
//			if( this.mmt_character.actTarget)this.mmt_character.actTarget.isSelect=false;
			
			while( actTargets.length > 0){
				var target:MapTarget	=	actTargets.shift() as MapTarget;
				
				if(target!=null && target.mapGrid!=null && target.gameData!=null) 
				target.isSelect	=	false;
				target.isWaitSelect=false;
			}
//			this.mmt_character.actTarget	=	null;
		}
		
		/**
		 *加一个对像 
		 * @param target
		 * 
		 */
		public function addTarget(target:MapTarget):void
		{
//			target.isSelect	=	true;
//			trace("addTarget", (target.gameData as GD_Building_Sundry).sundryConfigObj.zt_other_name);
			target.isWaitSelect=true;
			actTargets.push(target);
			
		}
		
		private var nowAction:Boolean=false;
		/**
		 *更新，在对象空闲时 
		 * 
		 */
		public function update():void
		{
			//自动攻击
			if( autoRepairWall && this.actTargets.length<1)
			{this.setAutoTarget();GD_Player.instance.tweenCenter();return;}
			
			
			var j:int=0;
			for each(var mtt:MapTarget in this.actTargets)
			{
				if(mtt as MT_HavenWall && (mtt as MT_HavenWall).wallData.currentlife>=(mtt as MT_HavenWall).wallData.lifeMax)
				{
//					trace("之前"+this.actTargets.length);
					mtt.isWaitSelect=false;
					this.actTargets.splice(j,1);
//					trace("之后"+this.actTargets.length);
				}else{
					mtt.isWaitSelect=true;
				}
				j++;
			}
			
			//如果当前有行动。且行动时间已经到	再次执行linkAction 防止在linkAction中的
			// "if(this.mmt_character.infectionState || this.mmt_character.actionLocked){return;}"返回后，导止后面的没执行
			if(this.nowAction && linkTimer==null){linkAction(null);}		 
			else			
			if( linkTimer==null && !nowAction && this.actTargets.length>0){			
				linkTimer = new Timer( actionDis, 1);								//在actionDis时间后行动
				linkTimer.addEventListener(TimerEvent.TIMER, linkAction);
				linkTimer.start();
			}
		}
		/**
		 *连续行动 
		 * 
		 */
		public function linkAction(e:Event):void
		{
			
			if(linkTimer){
				linkTimer.removeEventListener(TimerEvent.TIMER, linkAction);
				linkTimer = null;
			}
			nowAction	=	true;
			
			if(this.mmt_character.infectionState || this.mmt_character.actionLocked){return;}
			
			if(actTargets.length>0){
				
				//				trace(actTargets.length);
				var mt:MapTarget	= actTargets.shift() as MapTarget;
				//				trace("aftershift",actTargets.length);
				while(mt!=null && (mt.mapGrid==null || mt.gameData==null) )
				{
					mt		=	actTargets.shift() as MapTarget;
				}
				
				if(mt){
					
					mt.isWaitSelect = false;
					
					
					if(this.mmt_character.characterData.characterState==2 && GD_Player.instance.friendAct<1){
						this.cleanTargets();
					}
					else
					if(this.mmt_character.characterData.gd_characterPlay.actPoint<1)
					{
//						this.actTargets	.length		=	0;
						this.cleanTargets();
						VP_FastBuy_actPoint.instance.check();
					}
					else{
						if(mt as MT_HavenWall )
						{
							if((mt as MT_HavenWall).wallData.currentlife<(mt as MT_HavenWall).wallData.lifeMax)
							{
								this.setAct(mt);
							}
						}else{
							this.setAct(mt);
						}
						
					}
				}
				
				this.nowAction	=	false;
				
			}
			
		}
		
		public function setAct(actTarget:MapTarget):void
		{
//			trace(actTargets.length,this.mmt_character.actionLocked,actTarget.actionEnabled);
			
			if(this.mmt_character.infectionState || this.mmt_character.actionLocked || !actTarget.actionEnabled){return;}
			this.mmt_character.actionInterrupt();
			this.mmt_character.actTarget=actTarget;
			var targetPoint:Point=this.mmt_character.actTarget.mapGrid.coordinatePoint;
			
			this.mmt_character.findingTo(targetPoint);
		}
		
		/**
		 * 角色设置攻击对象的时间处理。 
		 * @return true为处理了
		 * 
		 */
		public function setActHandler(actTarget:MapTarget):Boolean
		{
			if(mmt_character.actTarget!=null || actTargets.length>0){													//加入连续行动
				addTarget( actTarget);
				return true;
			}
			
			return false;
		}
		
		
		static public  var autoRepairWall:Boolean	=	false;
//		public function set autoRepairWall(value:Boolean):void
//		{
//			this._autoRepairWall	=	value;
//		}
//		public function get autoRepairWall():Boolean
//		{
//			return this._autoRepairWall;
//		}
		public function setAutoTarget():void
		{
			var act:Number=-1;
			act =	mmt_character.characterData.gd_characterPlay.actPoint;	
			//				
			//				if(mmt_character.characterData.gd_characterPlay.playerID==GD_Player.instance.playerID)
			//				act	=	GD_Player.instance.actPoint;
			//				else
			//				act =	mmt_character.characterData.gd_characterPlay.actPoint;	
			
			if(mmt_character.infectionState || mmt_character.characterData.life<2){
				var eligibleGridsAry:Array=GV_Map.instance.getEligibleGridsAry(["避难所内","没有建筑","没有围墙","没有NPC"]);
				var randomGrid:GD_MapGrid;
				if(!eligibleGridsAry.length)throw new GameError("没有足够的位置放置对象");
				var randomIndex:int=Math.random()*eligibleGridsAry.length;
				randomGrid=eligibleGridsAry.splice(randomIndex,1)[0];
				mmt_character.setMove(randomGrid.coordinatePoint);
			}
			var actZ:MN_Zombie=null;
			var minDis:Number=10000;
			var i:uint;
			
			for each(var jsd:GD_Zombie in GV_Map.instance.mapData.zombiesObj)
			{
				var js:MN_Zombie = jsd.view;
				if(js!=null)
				{
					var point1:Point	=	mmt_character.mapGrid.coordinatePoint;
					var point2:Point	=	js.mapGrid.coordinatePoint;
					var dis:Number=GV_Map.instance.getFindingPriceBetweenTwoPoints(point1,point2);
					if(minDis>dis){
						minDis	=	dis;
						actZ	=	js;
					}
				}
			}
			
			if(actZ)this.addTarget(actZ);
		}
		
	}
}		


	




